package com.example.sachon;

import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class SA0300 extends TabActivity implements OnTabChangeListener{
	
	public static Context mContext;
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
	private static final String LOGTAG = "SA0300_";
    TabHost tab_host;
    Intent It; 
    String aTabSpec[] = {"소식지","장터","일정"};
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sa0300);
       
        
        init_title();
        
        //	SaUserInfo.getInstance().getPhone_number();
        // 이 탭액티비티에서 사용할 수 있는 TabHost 객체를 얻는다. 
           tab_host = getTabHost();  
           // 각 탭에 사용할 TabSpec 객체.
           for(int i = 0; i < 3; i++)
           {
           switch(i)
           {
           case 0:
           It = new Intent(this, SA1100.class);
           break;
           case 1:
           It = new Intent(this, SA2100.class);
           break;
           case 2:
           It = new Intent(this, SA3100.class);
           break;
          
           }
         // tabHost 생성
           tab_host.addTab(
        		tab_host.newTabSpec(aTabSpec[i])
               .setIndicator(aTabSpec[i])
               .setContent(It)
         );
	   // TabHost 높이
	   tab_host.getTabWidget().getChildAt(i).getLayoutParams().height = 90;
	   // TabHost 배경
	   tab_host.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
	           }
	   // debug Tab
	           
	   TextView tv = (TextView) tab_host.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
	   tv.setTextColor(Color.parseColor("#00CC00"));
	   tv.setGravity(Gravity.CENTER_HORIZONTAL);
	   tab_host.setCurrentTab(0);
	   // Look at Tab Change
	   tab_host.setOnTabChangedListener(this);
                
    }
    
 // 선택된 Tab에 대한 처리
    
	public void onTabChanged(String tabId )
    {
	    
	    Log.d(LOGTAG, "Tab_Node : "+ tabId);
        
	    //선택된 탭의 스타일 처리
	    for(int i=0;i<aTabSpec.length;i++)
	    {
	    	TextView tv = (TextView) tab_host.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.parseColor("#FFA9A9A9"));
            tv.setGravity(Gravity.CENTER_HORIZONTAL);
            
	    	if(aTabSpec[i]==tabId)
	        {
	            /**
	            * 글자색 변경
	            */
	        
	            tv.setTextColor(Color.parseColor("#00CC00"));
	            
	            /*
	            * 배경색 변경
	            */
	    		//tab_host.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000011"));
	            
	    		//break;
	        }
	    }
    }
    
    
    private void init_title()
    {
    	//setTitle(Const.getInstance().chonInfo().getChon_nm());
    	final TextView title_name = (TextView)findViewById(R.id.sa0300_title_txt_chon_name);
    	final ImageView btn_go_home = (ImageView)findViewById( R.id.sa0300_title_img_go_home);
    	final ImageButton btn_go_write = (ImageButton)findViewById( R.id.sa0300_title_img_go_board_write);
    	final ImageButton btn_refresh  = (ImageButton)findViewById( R.id.sa0300_title_img_board_refresh);
    	
    	title_name.setText(Const.getInstance().chonInfo().getChon_nm());
    	
    	btn_go_home.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(SA0300.this, SA0200.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(intent);
				finish();
			}
		});
    	
    	btn_refresh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				refresh();
			}
		});
    	
    	
    	btn_go_write.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				
				System.out.println("tab_host_selected::"+tab_host.getCurrentTab()); 
				switch ( tab_host.getCurrentTab() ){
				case 0:
					intent.setClass(SA0300.this, SA1400.class);
					break;
				case 1:
					intent.setClass(SA0300.this, SA2400.class);
					break;
				case 2:
					intent.setClass(SA0300.this, SA3400.class);
					break;
				}
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivityForResult(intent, 1);
			}
		});
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	 
    	System.out.println("okkkk:결과리턴됨");
    	refresh();
    }
    
    private void refresh()
    {
    	System.out.println("::refresh");
    	TabHost tabhost = getTabHost();

    	
    	
    	
		switch ( tab_host.getCurrentTab() ){
		case 0:
			
			Const.getInstance()
			.getRefreshableTab(SA1100.NAME).refresh();
 
			break;
		case 1:
			Const.getInstance()
			.getRefreshableTab(SA2100.NAME).refresh();
			break;
		case 2:
			Const.getInstance()
			.getRefreshableTab(SA3100.NAME).refresh();
			break;
		}	
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
